using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Skinner
{
    public partial class ImagePalette : UserControl
    {
        public ImagePalette()
        {
            InitializeComponent();
        }

        private StyleSheet _sheet = null;
        private HtmlDocument _doc = null;

        public ImagePalette(StyleSheet sheet, HtmlDocument doc)
        {
            InitializeComponent();
            _sheet = sheet;
            _doc = doc;

            int i = 0;
            foreach (KeyValuePair<string, string> c in _sheet.BackgroundImages)
            {
                if(AddColorControls(i, c.Key))
                    i++;
            }
            this.Height = i * 38 + 38;
        }

        private bool AddColorControls(int position, string key)
        {
            int height = 66;
            int width = 198;
            int topPad = 5;
            int leftPad = 5;
            int topOffset = 0;
            int leftOffset = 5;

            string backgroundImage = _sheet.BackgroundImages[key];

            LinkLabel imageUrl = new LinkLabel();
            PictureBox img = new PictureBox();

            try
            {
                if (backgroundImage.ToLower() != "none")
                {
                    imageUrl.Location = new Point(leftPad, ((topPad * position) == 0 ? topPad + topOffset : (topPad * (position + 1)) + (height * position) + topOffset));
                    imageUrl.Size = new Size(width, height);
                    imageUrl.Text = backgroundImage;
                    imageUrl.LinkClicked += new LinkLabelLinkClickedEventHandler(LinkClicked);

                    Controls.Add(imageUrl);
                   
                    backgroundImage = FixBackgroundImage(backgroundImage);
                    img.Location = new Point(leftPad + width + leftOffset, ((topPad * position) == 0 ? topPad + topOffset : (topPad * (position + 1)) + (height * position) + topOffset));
                    img.Size = new Size(width, height);
                    img.LoadAsync(backgroundImage);
                    Controls.Add(img);

                    img.Tag = key;
                    imageUrl.Tag = img;

                    return true;
                }
            }
            catch { }

            return false;
        }

        private string FixBackgroundImage(string backgroundImage)
        {
            if (backgroundImage.ToLower().Substring(0, 4) == "url(")
            {
                backgroundImage = backgroundImage.Substring(4);
            }

            backgroundImage = backgroundImage.Replace(")", "");

            //if the url starts with / it it relative to the root
            if (backgroundImage.Substring(0, 1) == "/")
            {
                backgroundImage = _doc.Url.Scheme + "://" + _doc.Url.Host + backgroundImage;
            }
            //maybe it's on the local disk.
            else if (backgroundImage.Substring(1,2) == @":\")
            {
                //do nothing else!
            }
            //otherwise it is relative to the css location.
            else
            {
                if(_sheet.Sheet.href != string.Empty)
                    backgroundImage = _doc.Url.Scheme + "://" + _doc.Url.Host + "/" + _sheet.Sheet.href.Substring(0, _sheet.Sheet.href.LastIndexOf("/")) + "/" + backgroundImage;
            }
            return backgroundImage;
        }

        private void LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ImageView view = new ImageView(FixBackgroundImage(((LinkLabel)sender).Text), ((LinkLabel)sender).Text, (string)((PictureBox)((LinkLabel)sender).Tag).Tag, _sheet);
                        
            view.ShowDialog();
            if (view.URL != ((LinkLabel)sender).Text && view.openFileDialog.FileName != string.Empty)
            {
                ((LinkLabel)sender).Text = view.URL;
                ((PictureBox)((LinkLabel)sender).Tag).Load(view.openFileDialog.FileName);
            }
        }
    }
}
